package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseHttpResult;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqIdBean;
import net.wsfcp.android.bean.requestbean.ReqOrderBean;
import net.wsfcp.android.bean.resultbean.OrderBean;
import net.wsfcp.android.bean.resultbean.OrderQuantityBean;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ErrorType;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.http.UrlHelper;
import net.wsfcp.android.utils.SpUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class OrderListModel extends BaseModel {

    //获取订单列表
    public void getOrderList(@NonNull int page, @NonNull int pageSize, int status, @NonNull final OrderListInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqOrderBean bean = new ReqOrderBean(page, pageSize, status, UrlHelper.CLIENT_TYPE);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.getOrderList(UrlHelper.GET_ORDER_LIST + SpUtils.getInstance().getToken(), body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<OrderBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(OrderBean orderBean) {
                        infoHint.getOrderListSuccessInfo(orderBean);
                    }

                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.getOrderListFailInfo(e.message);


                    }
                });

    }

    //取消订单
    public void cancelOrder(@NonNull String id, @NonNull final CancelOrderInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqIdBean bean = new ReqIdBean(id);
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.cancelOrder(UrlHelper.CANCEL_ORDER + SpUtils.getInstance().getToken(), body)
                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.cancelOrderSuccessInfo();
                            } else {
                                infoHint.cancelOrderFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.cancelOrderFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.cancelOrderFailInfo(e.getMessage());
                    }
                });


//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.cancelOrderSuccessInfo();
//                    }
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if(e instanceof ServerException)
//                        infoHint.cancelOrderFailInfo(((ServerException)e).message);
//
//                    }
//                });

    }


    //确认收货
    public void confirmReceipt(@NonNull String id, @NonNull final ConfirmReceiptInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqIdBean bean = new ReqIdBean(id);
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.confirmReceipt(UrlHelper.CONFIRM_RECEIPT + SpUtils.getInstance().getToken(), body)
                .enqueue(new Callback<BaseHttpResult>() {
                    @Override
                    public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                        if (response != null && response.body() != null) {
                            if (response.body().getResultCode() == ErrorType.SUCCESS) {
                                infoHint.confirmReceiptSuccessInfo();
                            } else {
                                infoHint.confirmReceiptFailInfo(response.body().getResultMsg());
                            }
                        } else {
                            infoHint.confirmReceiptFailInfo("失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseHttpResult> call, Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.confirmReceiptFailInfo(e.getMessage());
                    }
                });


//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.confirmReceiptSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if(e instanceof ServerException)
//                        infoHint.confirmReceiptFailInfo(((ServerException)e).message);
//
//                    }
//                });

    }


    //获取订单数量
    public void getOrderQuantity(@NonNull final GetOrderQuantityInfoHint
                                         infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");
        httpService.getOrderQuantity(UrlHelper.GET_ORDER_QUANTITY + SpUtils.getInstance().getToken()).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<OrderQuantityBean>(BaseApplication.getContext()) {
                    @Override
                    public void accept(OrderQuantityBean orderQuantityBean) {
                        infoHint.getOrderQuantitySuccessInfo(orderQuantityBean);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.getOrderQuantityFailInfo(e.message);

                    }
                });

    }


    //获取订单详情
    public void getOrderDetail(@NonNull String id, @NonNull final GetOrderDetailInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");

        ReqIdBean bean = new ReqIdBean(id);
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);


        httpService.getOrderDetail(UrlHelper.GET_ORDER_DETAIL + SpUtils.getInstance().getToken(), body).compose(new CommonTransformer())
                .subscribe(new CommonSubscriber<OrderBean.OrderList>(BaseApplication.getContext()) {
                    @Override
                    public void accept(OrderBean.OrderList orderQuantityBean) {
                        infoHint.getOrderDetailSuccessInfo(orderQuantityBean);
                    }


                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
                    @Override
                    public void accept(Throwable t) {
                        ServerException e = ExceptionEngine.handleException(t);
                        infoHint.getOrderDetailFailInfo(e.message);
                    }
                });

    }


    public interface OrderListInfoHint {
        //获取订单列表
        void getOrderListSuccessInfo(OrderBean orderBean);

        void getOrderListFailInfo(String str);


    }

    public interface CancelOrderInfoHint {
        //取消订单
        void cancelOrderSuccessInfo();

        void cancelOrderFailInfo(String str);


    }


    public interface ConfirmReceiptInfoHint {
        //确认收货
        void confirmReceiptSuccessInfo();

        void confirmReceiptFailInfo(String str);


    }

    public interface GetOrderQuantityInfoHint {
        //获取订单数量
        void getOrderQuantitySuccessInfo(OrderQuantityBean orderQuantityBean);

        void getOrderQuantityFailInfo(String str);


    }

    public interface GetOrderDetailInfoHint {
        //订单详情
        void getOrderDetailSuccessInfo(OrderBean.OrderList bean);

        void getOrderDetailFailInfo(String str);


    }

}
